<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="16">
        <a-form-item label="名称" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }">
          <a-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入名称"></a-input>
        </a-form-item>
        </a-row>

        <a-row class="form-row" :gutter="8">
          <a-col :lg="12">
            <a-form-item label="每天播放时间" :label-col="{ span: 10 }" :wrapper-col="{ span: 14 }">
              <a-time-picker format="HH:mm"  :allowClear="true" :inputReadOnly="true"  placeholder="开始时间" v-decorator="[ 'startTime', validatorRules.startTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="至" :label-col="{ span: 1 }" :wrapper-col="{ span: 17 }">
              <a-time-picker format="HH:mm"  :allowClear="true" :inputReadOnly="true" placeholder="结束时间" v-decorator="[ 'endTime', validatorRules.endTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="16">
        <a-form-item label="单图播放时长(秒)" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }">
          <a-input-number v-decorator="[ 'timespan', validatorRules.timespan]" min="5" max="120" placeholder="请输入播放时长,最小5秒，最大120秒" style="width: 100%"/>
        </a-form-item>
        </a-row>
        <a-row class="form-row" :gutter="16">
          <a-col :lg="5">

          </a-col>
          <a-col :lg="12">
            <span style="color: #0000FF"><a-icon type="exclamation-circle" />每天播放开始时间、结束时间不填表示全天播放</span>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import moment from 'moment';
  export default {
    name: "GlrColumnModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        disableMixinCreated:true,
        validatorRules: {
          name: {rules: [
            {required: true, message: '请输入名称!'},
          ]},
          status: {rules: [
          ]},
          releaseDate: {rules: [
            ]},
          timespan: {rules: [
            {required: true, message: '请输入播放时长!'},
          ]},
          sysCompanyCode: {rules: [
          ]},
        },
        url: {
          add: "/glr/glrColumn/add",
          edit: "/glr/glrColumn/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add (record) {
        this.form.resetFields();

        this.model = Object.assign({}, record);
        this.model.timespan=20;
        this.model.status='N';
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','timespan'))
        })
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.startTime=moment(this.model.startTime,'HH:mm');
        this.model.endTime=moment(this.model.endTime,'HH:mm');
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'startTime','endTime','name','timespan'))
        })
      },
      changeTime (val, dateStrings, type) {
        if (type === 'startTime') {
          this.startTime = dateStrings
        } else {
          this.endTime = dateStrings
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            var startTime=that.form.getFieldValue("startTime");
            if(!startTime){
              startTime="00:00";
            }else{
              startTime=moment(startTime).format("HH:mm");
            }
            values.startTime=startTime;
            var endTime=this.form.getFieldValue("endTime");
            if(!endTime){
              endTime="23:59";
            }else{
              endTime=moment(endTime).format("HH:mm");
            }
            values.endTime=endTime;
            let formData = Object.assign(this.model, values);
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','status','releaseDate','timespan','sysCompanyCode'))
      },

      
    }
  }
</script>